package com.example.foo.api.web.ftt;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.GetMapping;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.account.AccountHelper;
import com.example.foo.api.boot.TokenHelper;
import com.example.foo.ftt.model.FttProfit;
import com.example.foo.ftt.model.FttUser;
import com.example.foo.ftt.query.FttProfitQuery;
import com.example.foo.ftt.service.FttProfitService;
import com.example.foo.ftt.service.FttUserService;

@org.springframework.web.bind.annotation.RestController
public class FttProfitAction {

	private final FttUserService fttUserService;
	private final FttProfitService fttProfitService;

	public FttProfitAction(FttUserService fttUserService,
			FttProfitService fttProfitService) {
		this.fttUserService = fttUserService;
		this.fttProfitService = fttProfitService;
	}

	@GetMapping("/api/fttprofits")
	public AjaxResult index(FttProfitQuery fttProfitQuery, Pageable pageable) {
		Long userId = TokenHelper.verifyToken();
		fttProfitQuery.setPeriodDay(AccountHelper.toPeriodDay(LocalDate.now()));
		PageResult<FttProfit> pageResult = fttProfitService.findFttProfits(fttProfitQuery, pageable);

		FttUser fttUser = fttUserService.findFttUserByUserId(userId);
		List<FttProfit> fttProfits = pageResult.getRows()
				.stream()
				.filter(t -> t.getFttLevel().ordinal() <= fttUser.getFttLevel().ordinal())
				.collect(Collectors.toList());

		return AjaxResult.successData(new PageResult<>(fttProfits, fttProfits.size()));
	}

}
